﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// MobiLib, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

namespace MobiLibrary
{
    public partial class MobiLib
    {
        //==================================================================================================================================
        //
        public class PRC_header : IMobiHeader
        {
            public bool IsDirty { get; internal set; }
            internal MemoryBlock mb;
            public MemoryBlock MB { get { return mb; } }
            //
            public string Name { get { return Bytes2String(mb.Buffer, 0, 32); } internal set { String2BytesWA(value, mb.Buffer, 0, 32); } }
            public short Flags { get { return Bytes2Short(mb.Buffer, 0x20); } }
            public short Version { get { return Bytes2Short(mb.Buffer, 0x22); } }
            public int CreationTime { get { return Bytes2Int(mb.Buffer, 0x24); } internal set { Int2Bytes(value, mb.Buffer, 0x24); } }
            public DateTime dtCreationTime { get { return Bytes2DateTime(mb.Buffer, 0x24); } internal set { DateTime2Bytes(value, mb.Buffer, 0x24); } }
            public int ModificationTime { get { return Bytes2Int(mb.Buffer, 0x28); } internal set { Int2Bytes(value, mb.Buffer, 0x28); } }
            public DateTime dtModificationTime { get { return Bytes2DateTime(mb.Buffer, 0x28); } internal set { DateTime2Bytes(value, mb.Buffer, 0x28); } }
            public int BackupTime { get { return Bytes2Int(mb.Buffer, 0x2c); } internal set { Int2Bytes(value, mb.Buffer, 0x2c); } }
            public DateTime dtBackupTime { get { return Bytes2DateTime(mb.Buffer, 0x2c); } internal set { DateTime2Bytes(value, mb.Buffer, 0x2c); } }
            public int ModNum { get { return Bytes2Int(mb.Buffer, 0x30); } }
            public int AppInfo { get { return Bytes2Int(mb.Buffer, 0x34); } }
            public int SortInfo { get { return Bytes2Int(mb.Buffer, 0x38); } }
            public int Type { get { return Bytes2Int(mb.Buffer, 0x3c); } internal set { Int2Bytes(value, mb.Buffer, 0x3c); } }
            public string sType { get { return Bytes2String(mb.Buffer, 0x3c, 4); } internal set { String2BytesWZ(value, mb.Buffer, 0x3c, 4); } }
            public int Creator { get { return Bytes2Int(mb.Buffer, 0x40); } internal set { Int2Bytes(value, mb.Buffer, 0x40); } }
            public string sCreator { get { return Bytes2String(mb.Buffer, 0x40, 4); } internal set { String2BytesWZ(value, mb.Buffer, 0x40, 4); } }
            public int UniqueIdSeed { get { return Bytes2Int(mb.Buffer, 0x44); } }
            public int NextRecordList { get { return Bytes2Int(mb.Buffer, 0x48); } }
            public short NumRecords { get { return Bytes2Short(mb.Buffer, 0x4c); } }
            //
            public int RecordDataOffset0 { get { return Bytes2Int(mb.Buffer, 0x4E); } }
            public byte RecordAttributes0 { get { return mb.Buffer[0x52]; } }
            public int UniqueID0 { get; private set; }
            //
            public int RecordDataOffset1 { get { return Bytes2Int(mb.Buffer, 0x56); } }
            public byte RecordAttributes1 { get { return mb.Buffer[0x56 + 4]; } }
            public int UniqueID1 { get; private set; }
            //
            //public int Record0Size { get; private set; }
            //
            public PRC_header(byte[] buff)
            {
                lock (olock)
                {
                    IsDirty = false;
                    if (!(buff.Length >= 78 + 8))
                    {
                    }
                    mb.Buffer = new byte[buff.Length]; Array.Copy(buff, 0, mb.Buffer, 0, buff.Length);
                    //
                }
                //
            }
            //
            internal void Store(MemoryBlock MetaBuffer)
            {
                lock (olock)
                {
                    Array.Copy(mb.Buffer, 0, MetaBuffer.Buffer, mb.Offset, mb.Length);
                }
            }
            //
            //
            public override string ToString()
            {
                lock (olock)
                {
                    string s = "PRC_header:\n";
                    s += string.Format("Name={0}\n", Name);
                    s += string.Format("Flags={0}\n", Flags);
                    s += string.Format("Version={0}\n", Version);
                    if (0 != CreationTime && -1 != CreationTime)
                    {
                        s += string.Format("CreationTime={0}\n", dtCreationTime);
                    }
                    if (0 != ModificationTime && -1 != ModificationTime)
                    {
                        s += string.Format("ModificationTime={0}\n", dtModificationTime);
                    }
                    if (0 != BackupTime && -1 != BackupTime)
                    {
                        s += string.Format("BackupTime={0}\n", dtBackupTime);
                    }
                    //s += string.Format("ModNum={0}\n", ModNum);
                    //s += string.Format("AppInfo={0}\n", AppInfo);
                    //s += string.Format("SortInfo={0}\n", SortInfo);
                    s += string.Format("Type={0}\n", sType);
                    s += string.Format("Creator={0}\n", sCreator);
                    //s += string.Format("UniqueIdSeed={0}\n", UniqueIdSeed);
                    //s += string.Format("NextRecordList={0},NumRecords={1}\n", NextRecordList, NumRecords);
                    return s;
                }
            }
            //
            public string Dump()
            {
                lock (olock)
                {
                    string s = "PRC_header: MB=" + mb.ToString() + "\n";
                    s += string.Format("Name={0}\n", Name);
                    s += string.Format("Flags={0}\n", Flags);
                    s += string.Format("Version={0}\n", Version);
                    s += string.Format("CreationTime={0},dtCreationTime={1}\n", CreationTime, dtCreationTime);
                    s += string.Format("ModificationTime={0},dtModificationTime={1}\n", ModificationTime, dtModificationTime);
                    s += string.Format("BackupTime={0},dtBackupTime={1}\n", BackupTime, dtBackupTime);
                    s += string.Format("ModNum={0}\n", ModNum);
                    s += string.Format("AppInfo={0}\n", AppInfo);
                    s += string.Format("SortInfo={0}\n", SortInfo);
                    s += string.Format("Type={0},sType={1}\n", Type, sType);
                    s += string.Format("Creator={0},sCreator={1}\n", Creator, sCreator);
                    s += string.Format("UniqueIdSeed={0}\n", UniqueIdSeed);
                    s += string.Format("NextRecordList={0},NumRecords={1}\n", NextRecordList, NumRecords);
                    return s;
                }
                //
            }
        };
        //
        //
        //==================================================================================================================================
    }//class
}//namespace
